//
//  PhotosController.m
//  Test
//
//  Created by 郭洪军 on 3/29/18.
//  Copyright © 2018 Guo Hongjun. All rights reserved.
//

#import "PhotosController.h"
#import "LGGetImageAsset.h"
#import "LGCollectionCell.h"
#define kSizeThumbnailCollectionView  ([UIScreen mainScreen].bounds.size.width-10)/4
#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height

@interface PhotosController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)UICollectionViewFlowLayout *layout;

@property (nonatomic, strong)NSArray<PHAsset *> *assets;

@end

@implementation PhotosController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.collectionView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setAlbumName:(NSString *)albumName {
    _albumName = albumName;
    
    _assets = [LGGetImageAsset getImageArrayWithAlbumName:_albumName];
}

#pragma mark - UICollectionView Delegate + Datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.assets.count ;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LGCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photoCellId" forIndexPath:indexPath];
    
    cell.showWhat = SHOWImage;

    PHImageRequestOptions *phImageRequestOptions = [[PHImageRequestOptions alloc] init];
    phImageRequestOptions.synchronous = YES;
    
    [[PHImageManager defaultManager] requestImageForAsset:self.assets[indexPath.row] targetSize:CGSizeZero contentMode:PHImageContentModeAspectFill options:phImageRequestOptions resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
        dispatch_async(dispatch_get_main_queue(), ^{
            cell.imageItem.image = result;
        });
    }];
    
    return cell;
}

#pragma mark - Lazy load
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.minimumLineSpacing = 2.0;
        _layout.minimumInteritemSpacing = 2.0;
        _layout.itemSize = CGSizeMake(kSizeThumbnailCollectionView, kSizeThumbnailCollectionView);
        _layout.sectionInset = UIEdgeInsetsMake(2, 2, 2, 2);
        _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _layout.footerReferenceSize = CGSizeMake(SCREEN_W, 40);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H-64) collectionViewLayout:self.layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        [_collectionView registerClass:[LGCollectionCell class] forCellWithReuseIdentifier:@"photoCellId"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.tag = 1000;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
    }
    return _collectionView;
}

@end
